'use strict';

const conn = require('../../libs/Conn');

module.exports = async function (params) {
    let querySql = `SELECT
                    ef.engineering_id,
                    ef.engineering_name,
                    ef.engineering_no,
                    DATE_FORMAT(ef.startDate,'%Y-%m-%d') AS startDate,
                    ef.building_id,
                    ef.structure_id,
                    ef.region_id,
                    uf.user_id AS principal_id,
                    uf.user_name AS principal_name,
                    ef.status
                FROM
                    engineering_file ef 
                LEFT JOIN user_file uf ON ef.principal_id = uf.user_id AND uf.delete_sign = 1
                WHERE
                    ef.delete_sign = 1
                AND ef.engineering_id = ?;
    SELECT
                engineering_area,
                engineering_address,
                aboveGround_floor,
                underGround_floor,
                DATE_FORMAT(admission_construction_date,'%Y-%m-%d') AS admission_construction_date,
                admission_construction_status,
                supervisory_registration_certificate,
                planning_certificate,
                drawing_number,
                construction_permit,
                construction_unit_employer_id,
                design_unit_id,
                construction_unit_contractor_id,
                supervisory_unit_id,
                survey_unit_id,
                drawing_unit_id,
                construction_unit_employer_person,
                design_unit_person,
                construction_unit_contractor_person,
                supervisory_unit_person,
                survey_unit_person,
                drawing_unit_person
        FROM
            engineering_information_file
        WHERE
            delete_sign = 1
        AND engineering_id = ?;
        SELECT
                eum.*,
                uf.user_name
            FROM
                engineering_user_middle eum 
	    LEFT JOIN user_file uf ON eum.user_id = uf.user_id AND uf.delete_sign = 1 
	    WHERE
	    eum.engineering_id = ?
        `;
    let rec = await conn(querySql,[params.engineering_id,params.engineering_id,params.engineering_id]);
    let dataObj = rec[0][0];
    for(let data in rec[1][0]){
        dataObj[data] = rec[1][0][data]
    }
    let userIds =[];
    let user_name ='';
    for(let i = 0; i<rec[2].length;i++){
        userIds.push(rec[2][i].user_id);
        if(i===rec[2].length - 1){
            user_name +=  rec[2][i].user_name
        }else{
            user_name +=  rec[2][i].user_name + ','
        }
    }
    dataObj.userIds=userIds;
    dataObj.user_name=user_name;
    return {success: true, data: dataObj};

};